package edu.hgnc.controller.fileUpload;

import edu.hgnc.controller.testController.Test01;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@Controller
public class FileUploadController{

	private static Log logger = LogFactory.getLog(FileUploadController.class);

	@RequestMapping("/fileUpload")
	public String upload(MultipartFile multipartFile, String desc, HttpServletRequest request) throws Exception{
		logger.warn(desc);
		if (multipartFile.isEmpty()){
			logger.info("文件体为空");
			return "文件体为空";
		}
		String path = request.getServletContext().getRealPath("/WEB-INF/file");
		String filename = multipartFile.getOriginalFilename();
		File file = new File(path, filename);
		// 文件夹不存在
		if (!file.getParentFile().exists()){
			file.getParentFile().mkdir();
		}
		multipartFile.transferTo(file);
		return "上传成功";
	}
}
